import 'dart:developer';

import 'package:ball_lion/widgets/base/wqs_carousel_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/sp_utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/entity/other/resource_entity.dart';
import 'package:ball_lion/server/api/other_api.dart';

class AdsLabel extends StatefulWidget {
  const AdsLabel(
      {super.key,
      required this.adsTag,
      this.margin = const EdgeInsets.fromLTRB(8, 0, 8, 8),
      this.width = 359,
      this.height = 72,
      this.close,
      this.onClose,
      this.tapType,
      this.tagParam});

  final String adsTag;
  final EdgeInsets margin;
  final double width;
  final int? close;
  final double height;
  final String? tapType;
  final String? tagParam;
  final Function()? onClose;

  @override
  State<AdsLabel> createState() => _AdsLabelState();
}

class _AdsLabelState extends State<AdsLabel>
    with AutomaticKeepAliveClientMixin {
  List<ResourceEntity> carousel = [];

  @override
  void initState() {
    getData();
    super.initState();
  }

  @override
  void didUpdateWidget(covariant AdsLabel oldWidget) {
    if (oldWidget.adsTag != widget.adsTag) getData();
    super.didUpdateWidget(oldWidget);
  }

  Future getData() async {
    carousel = [];
    List<ResourceEntity>? list = await OtherApi.getAppList("app_ads");
    list?.forEach((element) {
      if (element.button == widget.adsTag) {
        setState(() {
          log("${widget.adsTag}---------${element.button}");
          carousel.add(element);
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return carousel.isEmpty
        ? Container()
        : Container(
            width: Get.width,
            margin: widget.margin,
            child: Stack(children: [
              QsCarouselView(
                padding: 0,
                aspectRatio: widget.width / widget.height,
                carousels: carousel,
                needDot: carousel.length != 1,
                needTitle: false,
                tapType: widget.tapType,
                tagParam: widget.tagParam,
              ),
              if (widget.close != null)
                Positioned(
                    top: 4,
                    right: 4,
                    child: Container(
                            width: 16,
                            height: 16,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              color: Colors.white.withOpacity(0.2),
                              shape: BoxShape.circle,
                            ),
                            child: Image.asset(
                                width: 16,
                                color: Colours.white,
                                Utils.getImgPath("close_icon_circle.png")))
                        .tap(() {
                      if (widget.adsTag.contains("match_lq"))
                        SpUtils.setMatchLqAds(
                            widget.close!, DateTime.now().toIso8601String());
                      if (widget.adsTag.contains("match_zq"))
                        SpUtils.setMatchZqAds(
                            widget.close!, DateTime.now().toIso8601String());
                      widget.onClose?.call();
                    }))
            ]));
  }

  @override
  bool get wantKeepAlive => true;
}
